package gov.nist.applet.phone.ua.gui;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JDialog;

public class ChatDialog extends JDialog {

	private static final long serialVersionUID = 1L;

	private int LocationY = 0;

	private JPanel contentPane = null;
	private JLabel jlAccountInfo = null;
	private JPanel jpVideoShow = null;
	private JLabel jlCallStatus = null;
	private JPanel jpVolumn = null;

	private JPanel jpControls = null;
	private JButton jbHangup = null;
	private JButton jbNoAudio = null;
	private JButton jbNoVideo = null;
	private JButton jbEncryptEnabled = null;

	/**
	 * @param owner
	 */
	public ChatDialog(Frame owner, boolean justAudio) {
		super(owner);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(500, 400);
		this.setTitle("00:00:00");
		this.setLayout(null);
		this.setContentPane(getContentPanel());
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);// 显示在屏幕中央
		setResizable(false);
		setVisible(true);
	}

	private JPanel getContentPanel() {
		contentPane = new JPanel() {
			private static final long serialVersionUID = 1L;

			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				ImageIcon img = new ImageIcon(
						ChatDialog.class
								.getResource("/images/chatDialog/chatDialog.png"));
				g.drawImage(img.getImage(), 0, 0, null);
			};
		};
		contentPane.setBackground(Color.WHITE);
		contentPane.setLayout(null);
		contentPane.add(getAccountInfo());
		contentPane.add(getJpVideoShow());
		contentPane.add(getJlCallStatus());
		contentPane.add(getJpVolumn());
		contentPane.add(getJpControls());
		return contentPane;
	}

	private JLabel getAccountInfo() {
		if (jlAccountInfo == null) {
			jlAccountInfo = new JLabel("账户信息", SwingConstants.CENTER);
			jlAccountInfo.setSize(500, 20);
			jlAccountInfo.setText("账户信息");
			jlAccountInfo.setLocation(0, LocationY);
			LocationY += jlAccountInfo.getHeight();
		}
		return jlAccountInfo;
	}

	private JPanel getJpVideoShow() {
		jpVideoShow = new JPanel();
		jpVideoShow.setSize(500, 210);
		jpVideoShow.setLocation(0, LocationY);
		// jpVideoShow.setBackground(Color.BLUE);
		jpVideoShow.setOpaque(false);
		LocationY += jpVideoShow.getHeight();
		return jpVideoShow;
	}

	private JLabel getJlCallStatus() {
		if (jlCallStatus == null) {
			jlCallStatus = new JLabel("通话状态", SwingConstants.CENTER);
			jlCallStatus.setSize(500, 20);
			jlCallStatus.setText("通话状态");
			jlCallStatus.setLocation(0, LocationY);
			LocationY += jlCallStatus.getHeight();
		}
		return jlCallStatus;
	}

	private JPanel getJpVolumn() {
		jpVolumn = new JPanel();
		jpVolumn.setSize(500, 70);
		jpVolumn.setLocation(0, LocationY);
		jpVolumn.setOpaque(false);
		LocationY += jpVolumn.getHeight();
		return jpVolumn;
	}

	private JPanel getJpControls() {
		jpControls = new JPanel();
		jpControls.setSize(500, 50);
		jpControls.setLocation(0, LocationY);
		jpControls.setOpaque(false);
		jpControls.setLayout(null);

		// Core Design
		ControlButtonMouseAdapter adapter = new ControlButtonMouseAdapter();

		jbHangup = new JButton(
				new ImageIcon(
						ChatDialog.class
								.getResource("/images/chatDialog/controls/hangupButton.png")));
		jbHangup.setPressedIcon(new ImageIcon(
				ChatDialog.class
						.getResource("/images/chatDialog/controls/hangupButtonPressed.png")));
		jbHangup.setSize(29, 29);
		jbHangup.setLocation(450, 10);
		jbHangup.setOpaque(false);
		jbHangup.addMouseListener(adapter);
		jpControls.add(jbHangup);

		jbNoAudio = new JButton(
				new ImageIcon(
						ChatDialog.class
								.getResource("/images/chatDialog/controls/soundOff.png")));
		jbNoAudio.setSize(30, 30);
		jbNoAudio.setLocation(10, 10);
		jbNoAudio.setOpaque(false);
		jbNoAudio.addMouseListener(adapter);
		jpControls.add(jbNoAudio);

		jbNoVideo = new JButton(
				new ImageIcon(ChatDialog.class
						.getResource("/images/chatDialog/controls/noVideo.png")));
		jbNoVideo.setSize(30, 30);
		jbNoVideo.setLocation(45, 10);
		jbNoVideo.setOpaque(false);

		jbNoVideo.addMouseListener(adapter);
		jpControls.add(jbNoVideo);

		jbEncryptEnabled = new JButton(
				new ImageIcon(ChatDialog.class
						.getResource("/images/chatDialog/controls/encrypt.png")));
		jbEncryptEnabled.setSize(30, 30);
		jbEncryptEnabled.setLocation(80, 10);
		jbEncryptEnabled.setOpaque(false);
		jbEncryptEnabled.addMouseListener(adapter);
		jpControls.add(jbEncryptEnabled);

		return jpControls;
	}

	class ControlButtonMouseAdapter extends MouseAdapter {
		public void mouseClicked(MouseEvent event) {
			if (event.getSource().equals(jbHangup)) {
				System.out.println("Hangup");
			} else if (event.getSource().equals(jbNoAudio)) {
				System.out.println("No Audio");
			} else if (event.getSource().equals(jbNoVideo)) {
				System.out.println("No Video");
			}
		}
	}

	public static void main(String[] args) {
		new ChatDialog(new MainFrame(), true);
	}

}
